package cn.yx.common.core.enums;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>响应Code枚举</p>
 *
 * @author Wgssmart
 */
public enum ResponseCodeEnum {

    /**
     * 成功
     */
    OK(1001, "cn.yx.common.core.success"),
    /**
     * 失败
     */
    FAIL(1002, "cn.yx.common.core.fail"),
    /**
     * 参数错误
     */
    DATA_ERROR(1003, "cn.yx.common.core.dataError"),
    /**
     * 服务错误，如限流，熔断，降级等
     */
    SERVICE_ERROR(1004, "cn.yx.common.core.serviceError"),
    /**
     * 认证异常
     */
    AUTHENTICATION_ERROR(1005, "cn.yx.common.security.authenticationError"),
    /**
     * 授权异常
     */
    AUTHORIZATION_ERROR(1006, "cn.yx.common.security.authorizationError");

    private final Integer code;
    private final String message;

    private static final Map<Integer, ResponseCodeEnum> LOOKUP;

    static {
        LOOKUP = new HashMap<>();
        for (ResponseCodeEnum responseStatusEnum : EnumSet.allOf(ResponseCodeEnum.class)) {
            LOOKUP.put(responseStatusEnum.getCode(), responseStatusEnum);
        }
    }

    ResponseCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static ResponseCodeEnum findByCode(Integer code) {
        return LOOKUP.get(code);
    }
}
